/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.lists;

import aic.beans.Customer;
import aic.beans.Order;
import aic.misc.DebugPrint;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class CustomerList {

    private static List<Customer> customerList = Collections.synchronizedList(new LinkedList<Customer>());

    public static boolean add(Customer customer) {
        DebugPrint.println("adding Customer: " + customer);
        return customerList.add(customer);
    }

    public static boolean contains(Customer customer) {
        boolean ret = customerList.contains(customer);
        DebugPrint.println("Customer " + customer + " is in the list: " + ret);
        return ret;
    }

    public static Customer getCustomer(String id) {
        for(Customer customer : customerList){
            if(customer.getId().equals(id)){
                DebugPrint.println("Customer with id " + id + " is in the list.");
                return customer;
            }
        }
        DebugPrint.println("Customer with id " + id + " is not in the list.");
        return null;
    }

    public static boolean updateCustomer(Customer customer) {
        for(int i=0; i < customerList.size(); i++){
            if(customerList.get(i).getId().equals(customer.getId())){
                customerList.set(i, customer);
                DebugPrint.println("Customer " + customer + " is updated.");
                return true;
            }
        }
        DebugPrint.println("Customer " + customer + " is not in the list.");
        return false;
    }

    public static boolean deleteCustomer(String id) {
        for(int i=0; i < customerList.size(); i++){
            if(customerList.get(i).getId().equals(id)){
                customerList.remove(i);
                DebugPrint.println("Customer with id " + id + " is deleted form the list.");
                printList();
                return true;
            }
        }
        DebugPrint.println("Customer with id " + id + " is not in the list.");
        return false;
    }

    public static boolean add_order(Customer customer, Order order) {
        DebugPrint.println("Adding an order to a customer.. (Customer: " + customer + " // Order: " + order + ")");
        try {
            printList();
            Customer Customer = customerList.get(customerList.indexOf(customer));
            List<Order> tmpOrdersList = Customer.getOrders();
            tmpOrdersList.add(order);
            DebugPrint.println("..done adding an order to a customer.");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            DebugPrint.println("..wasn't able to add an order to a customer.");
            return false;
        }
    }

    /**
     * Prints out a list of all Customers within "customerList"
     */
    public static void printList() {
        Iterator it = customerList.iterator();

        while (it.hasNext()) {
            DebugPrint.println(">>list value (customerList): " + it.next());
        }
    }
}
